﻿// 大名科技（天津）有限公司 版权所有
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动
//
// 任何基于本项目二次开发而产生的一切法律纠纷和责任，均与作者无关

using Admin.NET.Application.Entity;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using Org.BouncyCastle.Utilities;
using RazorEngine.Compilation.ImpromptuInterface.Dynamic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Admin.NET.Core.Util;
public class SocketUtil
{
    public static ReceiveData formatData(byte[] data)
    {
        ReceiveData r = new ReceiveData();
        int index = 0;
        r.Version = Util.ReadByte(data, ref index);
        r.Cmd = Util.ReadByte(data, ref index);
        r.RouteId = Util.ReadINT32(data, ref index);
        r.BlindId = Util.ReadINT32(data, ref index);
        r.Blank = Util.ReadShort(data, ref index);
        r.Pipe = Util.ReadByte(data, ref index);
        r.Snr = Util.ReadByte(data, ref index);
        r.Rssi = Util.ReadByte(data, ref index);
        r.Rssiflag = Util.ReadByte(data, ref index);
        r.Blank2 = Util.ReadShort(data, ref index);
        r.Timetick = Util.ReadINT32(data, ref index);
        r.Online = Util.ReadByte(data, ref index);
        r.Onnet = Util.ReadShort(data, ref index);
        r.Datalen = Util.ReadShort(data, ref index);
        r.Data2 = Util.ReadData(data, ref index, (int)r.Datalen, null);
        r.Data = Util.Bytes2Hex(Util.getBytes(data, index, (int)r.Datalen));//Util.ReadString(data, ref index, (int)r.Datalen,null);
        r.EventTime = Util.UnixToTime((int)r.Timetick);
        r.AllData = Util.Bytes2Hex(data);
        return r;
    }
    
}

public static class ReceiveDataExtensions
{
    public static string toStringData(this ReceiveData data)
    {
        Type myType = data.GetType();
        PropertyInfo[] properties = myType.GetProperties();
        string rtn = "";
        foreach (PropertyInfo property in properties)
        {
            rtn += string.Format("Name: {0}, Type: {1}, Value: {2}\n", property.Name, property.PropertyType.Name, property.GetValue(data, null));
        }
        return rtn;
    }
}
public class Util
{
    public static int INT32 = 4;
    public static int INT16 = 2;
    public static int BYTE8 = 1;
    public static Byte[] getBytes(Byte[] data, int start, int length)
    {
        byte[] result = new byte[length];
        for (int i = 0; i < length; i++)
        {
            result[i] = data[start + i];
        }
        return result;
    }
    public static int ReadINT32(byte[] data, ref int index)
    {
        byte[] bytes = Util.getBytes(data, index, Util.INT32);
        //if (BitConverter.IsLittleEndian)
            Array.Reverse(bytes);
        int result = BitConverter.ToInt32(bytes, 0);
        index += Util.INT32;
        return result;
    }
    public static short ReadShort(byte[] data, ref int index)
    {
        byte[] bytes = Util.getBytes(data, index, Util.INT16);
        Array.Reverse(bytes);
        short result = BitConverter.ToInt16(bytes, 0);
        index += Util.INT16;
        return result;
    }
    public static byte ReadByte(byte[] data, ref int index)
    {
        byte result = data[index];
        index += Util.BYTE8;
        return result;
    }
    public static byte[] short2Byte(short v)
    {
        return BitConverter.GetBytes(v);
    }
    public static byte[] Int2Byte(int v)
    {
        return BitConverter.GetBytes(v);
    }
    public static byte[] String2Bytes(string s, Encoding coding)
    {
        if (coding == null) coding = Encoding.UTF8;
        return coding.GetBytes(s);
    }
    public static string ReadString(byte[] data, ref int index, int len, Encoding coding)
    {
        if (coding == null) coding = Encoding.UTF8;
        //string result = BitConverter.ToString(Util.getBytes(data, index, len));
        string result = coding.GetString(Util.getBytes(data, index, len));
        index += len;
        return result;
    }
    public static string ReadData(byte[] data, ref int index, int len, Encoding coding)
    {
        string result = "";
        for(var j = 0; j < len; j++)
        {
            if (result != "") result += ",";
            string r = ByteToBitArray(data[index + j]);
            result += r;
        }
        return result;
    }
    public static string Bytes2Hex(byte[] bytes)
    {
        string hexString = "";
        foreach (byte b in bytes)
        {
            if (hexString != "") hexString += " ";
            hexString += b.ToString("X2");
        }
        return hexString;
    }
    public static byte[] StrHex2Bytes(string ss, string split)
    {

        if (split != null)
        {
            ss = ss.Replace(split, "");
        }
        List<byte> bytes = new List<byte>();
        for (var i = 0; i < ss.Length / 2; i++)
        {
            byte bat = Convert.ToByte(ss.Substring(i * 2, 2), 16);
            bytes.Add(bat);
        }
        return bytes.ToArray();

    }
    public static DateTime UnixToTime(int date)
    {

        double num = (double)date;
        double value = Convert.ToDouble(num);
        DateTime dateTime = new DateTime(1970, 1, 1);
        dateTime = dateTime.AddHours(8.0);
        return dateTime.AddSeconds(value);
    }
    // 将byte转换为一个包含8个bit的字符串数组
    public static string ByteToBitArray(byte b)
    {
        string binaryString = Convert.ToString(b, 2);
        if (binaryString.Length < 8)
        {
            binaryString = binaryString.PadLeft(8, '0');
        }
        return binaryString;
    }
    public static int DateTimeToUnixTimeStamp(System.DateTime dateTime)
    {
        // 转换为UTC时间，减去时间的毫秒数，然后除以1000得到Unix时间戳
        return (int)((dateTime.ToUniversalTime().Ticks - 621355968000000000) / 10000000);
    }
}
